package com.yaoli.niuke;

import java.awt.print.Printable;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by siemens on 5/8/2017.
 */
public class _004_Test {
    public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {

        char matrixs [][] = new char[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrixs[i][j] = matrix[i*cols+j];
                System.out.print(matrixs[i][j]+"\t");
            }
            System.out.println();
        }

        //SLHE CCEI DEJF GGFIE

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if(matrixs[i][j] == str[0]){
                    check(matrixs,i,j,str,0);
                    if(flag == true){
                        break;
                    }
                }
            }
            if(flag == true){
                break;
            }
        }
        return flag;
    }

    boolean flag = false;
    Set<String> visisted = new HashSet<String>();

    public void check(char [][] matrix , int r ,int c , char [] str, int p){
        if(!visisted.contains(r+"-"+c)){
            visisted.add(r+"-"+c);
            if(p < str.length){
                if(matrix[r][c] == str[p] && p == str.length -1){
                    flag = true;
                    return;
                }
                if(matrix[r][c] == str[p]){
                    if(c > 0 ){
                        check(matrix,r,c-1,str,p+1);
                    }
                    if(c < matrix[0].length - 1){
                        check(matrix,r,c+1,str,p+1);
                    }
                    if(r > 0){
                        check(matrix,r-1,c,str,p+1);
                    }
                    if(r < matrix.length - 1){
                        check(matrix,r+1,c,str,p+1);
                    }
                }
            }
            visisted.remove(r+"-"+c);
        }

    }

    public static void main(String[] args) {
        char matrix[]= {
                'a', 'b' ,'c' ,'e',
                's' ,'f' ,'c' ,'s',
                'a' ,'d' ,'e' ,'e'
        };

        char ss []= (
                "ABCEHJIG" +
                "SFCSLOPQ" +
                "ADEEMNOE" +
                "ADIDEJFM" +
                "VCEIFGGS").toCharArray();

        int rows = 5;
        int cols = 8;

        char [] strs = "SLHECCEIDEJFGGFIEJ".toCharArray();
        char [] strs2 = "abcb".toCharArray();

        _004_Test idleReasonController = new _004_Test();
        System.out.print(idleReasonController.hasPath(ss,rows,cols,strs));
    }
}
